package cc.ufinity.networkdiagnose.view;

import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.test.Location;

import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

public class WelcomeActivity extends AbstractActivity {
	private int gpsOrNet = LocationClientOption.GpsFirst;
	public static LocationClient mLocClient;

	//private ProgressBar pbar;
	private TextView version;
	private String strVersion;
	private Handler handler;
	
	private Dialog waitDialog;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				if(msg.arg1 == 1) {
					showVersion();
				}else if(msg.what==1){
					mLocClient.stop();
					System.out.println("------得到经纬度关闭-----------");
				}
			}
		};		
		
		Location loc=new Location(getApplicationContext(),handler);
		mLocClient=loc.mLocationClient;
		setLocationOption();//设置一些参数
		//mLocClient.start();//开始获取经纬度
		version = (TextView)findViewById(R.id.version);
		//pbar = (ProgressBar) this.findViewById(R.id.pBar);
		//注册服务
		//Intent service = new Intent(this,WifiScanService.class);
		//super.startService(service);
		 
		new Thread() {
			public void run() {
				init();
			}
		}.start();
	}

	private void setLocationOption(){
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);				//��gps
		option.setCoorType("bd09ll");		//�����������
		option.setServiceName("com.baidu.location.service_v2.9");
		option.setScanSpan(1000);//���ö�λģʽ��С��1����һ�ζ�λ;���ڵ���1����ʱ��λ
		option.setAddrType("all");//���÷��ص�ַ
		option.setPriority(gpsOrNet);        //�����ã�Ĭ����gps����
//		option.setPoiNumber(10);
		option.disableCache(true);		
		mLocClient.setLocOption(option);
	}
	
	private void init() {
			delay();
	}
	
	private void enter(){
		Intent main = new Intent(WelcomeActivity.this, HomePageActivity.class);
		main.putExtra("intentFlag", "WelcomeActivity");
		startActivity(main);
		this.finish();
	}

	private void delay() {
		PackageManager packageManager = getPackageManager();
		//getPackageName()是你当前类的包名，0代表是获取版本信息
		PackageInfo packInfo = null;
		try {
			packInfo = packageManager.getPackageInfo(getPackageName(), 0);
		} catch (NameNotFoundException e) {}
		if(packInfo != null) {
			strVersion = packInfo.versionName; 
		}
		if(strVersion != null) {
			Message msg = handler.obtainMessage();
			msg.arg1 = 1;
			msg.sendToTarget();
			GlobalApp.version=strVersion;
			//version.setText("版本号：" + v);
		}

		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {}
		enter();
	
	}
	
	private void showVersion() {
    	waitDialog = new Dialog(WelcomeActivity.this, R.style.TRANSDIALOG);
        waitDialog.setContentView(R.layout.version);
        waitDialog.getWindow().setBackgroundDrawableResource(R.color.transparent2);
        version = (TextView)waitDialog.findViewById(R.id.version);
        version.setText("版本号：" + strVersion);
      //设置窗口里控件的透明度
        Window window = waitDialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
        window.setGravity(Gravity.CENTER_HORIZONTAL| Gravity.TOP);

        WindowManager m = WelcomeActivity.this.getWindowManager(); 
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用 
        //lp.x = (d.getWidth()- 200) / 2; // 新位置X坐标 480
        lp.y = (int) (d.getHeight() * 0.87); // 新位置Y坐标 800
        lp.alpha = 0.95f; // 透明度 
        window.setAttributes(lp); 
        waitDialog.setCancelable(false);
        waitDialog.setCanceledOnTouchOutside(false);
        waitDialog.show();
    }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if(null != waitDialog) {
			waitDialog.dismiss();
		}
		super.onDestroy();
	}

}
